package root.electronclass.electronclass.grade.member

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.fragment.app.viewModels
import root.electronclass.electronclass.R
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassMemberBinding
import root.electronclass.electronclass.databinding.FragmentClassMemberStudentItemBinding
import root.electronclass.electronclass.databinding.FragmentClassMemberTeacherItemBinding
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.doOnTabSelected
import root.electronclass.electronclass.ext.dp
import root.electronclass.electronclass.ext.showAvatar
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.grade.model.Person

/**
 * 班级成员墙
 * @author Lokiy
 * @date 2023/2/25 21:13
 */
class ClassMemberFragment : ViewBindingFragment<FragmentClassMemberBinding>() {

    private val viewModel by viewModels<ClassMemberViewModel>()

    private val teacherAdapter = BasicAdapter<Person, FragmentClassMemberTeacherItemBinding> { holder, item ->
        val binding = holder.binding
        binding.itemTitle.text = item.title.takeIf { it != "任课老师" } ?: item.tag
        binding.itemName.text = item.name
        binding.itemNo.text = getString(R.string.class_teacher_no, item.no)

        binding.itemAvatar.showAvatar(item.avatar)

        binding.itemSlogan.text = buildSpannedString {
            bold {
                append("个人格言：")
            }
            append(item.slogan.orEmpty().ifEmpty { "暂无数据" })
        }
        binding.itemDesc.text = buildSpannedString {
            bold {
                append("自我介绍：")
            }
            append(item.introduce.orEmpty().ifEmpty { "暂无数据" })
        }
        binding.root.setOnClickListener {
            startFragment(ClassMemberDetailFragment::class.java, bundleOf("_DATA" to item))
        }
    }
    private val studentAdapter = BasicAdapter<Person, FragmentClassMemberStudentItemBinding> { holder, item ->
        val binding = holder.binding
        binding.itemTitle.text = item.title.takeIf { it != "班干部" } ?: item.tag
        binding.itemName.text = item.name
        binding.itemNo.text = getString(R.string.class_student_no, item.no)

        binding.itemAvatar.showAvatar(item.avatar)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.recycler.addItemDecoration(GridSpacingItemDecoration(4, dp2px(40.dp).toInt(), false))
        binding.recycler.adapter = teacherAdapter

        binding.tabLayout.apply {
            resources.getStringArray(R.array.class_member_tabs).forEach {
                addTab(newTab().setText(it))
            }
            doOnTabSelected {
                if (it?.position == 0) {
                    binding.recycler.adapter = teacherAdapter
                } else {
                    binding.recycler.adapter = studentAdapter
                }
            }
        }
        binding.search.doOnSearch { key ->
            val predicate: (Person) -> Boolean = {
                it.name.lowercase().contains(key.lowercase()) || it.no.toString().lowercase().contains(key.lowercase()) || it.title.toString()
                    .lowercase().contains(key.lowercase()) || it.tag.toString().lowercase().contains(key.lowercase())
            }
            teacherAdapter.notifyChanged(viewModel.teacherList.value?.filter(predicate))
            studentAdapter.notifyChanged(viewModel.studentList.value?.filter(predicate))
            true
        }

        viewModel.teacherList.observe(viewLifecycleOwner) {
            teacherAdapter.notifyChanged(it)
        }
        viewModel.studentList.observe(viewLifecycleOwner) {
            studentAdapter.notifyChanged(it)
        }
    }
}